﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace Helpers.Extensions
{
    public static class JsonExtensions
    {
        public static string ToJsonStr(this object obj)
        {
            return JsonHelper.SerializeObject(obj);
        }

        public static T ToJsonObj<T>(this string json) where T : class
        {
            return JsonHelper.Deserialize<T>(json);
        }

        public static T ToJsonObj<T>(this string json, ref T anonymousTypeObject) where T : class
        {
            return JsonHelper.DeserializeAnonymousType<T>(json, ref anonymousTypeObject);
        }

        public static bool TryParseJson<T>(this string json, out T obj, T TanonymousTypeObject)
        {
            obj = default;
            try
            {
                obj = Helpers.JsonHelper.DeserializeAnonymousType<T>(json, ref TanonymousTypeObject);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}

